/*
 * Created on Jun 3, 2005
 *
 */
package com.erclab.internal.xpresso.handlers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import com.erclab.internal.xpresso.reportelements.*;

import org.apache.commons.dbutils.ResultSetHandler;

/**
 * @author Enrique Rodriguez.
 *
 */
public class ReportElementHandler implements ResultSetHandler {

	private void setReportElementProperties(ReportElement re, ResultSet rs) throws SQLException{
		
		re.setIdReportElement(rs.getString("IdReportElement"));
		re.setClassHandler(rs.getString("ClassHandler"));
		re.setColumnName(rs.getString("ColumnName"));
		re.setDisplayOrder(rs.getInt("DisplayOrder"));
		re.setExternalDataQuery(rs.getString("ExternalDataQuery"));
		re.setIdExternalDataSource(rs.getString("IdExternalDataSource"));
		re.setHeaderText(rs.getString("HeaderText"));
		re.setStyle(rs.getString("Style"));
		re.setIsTotalColumn(rs.getBoolean("IsTotalColumn"));
		re.setFlexClass(rs.getString("FlexClass"));
		re.setParameters(rs.getString("Parameters"));
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
	 */
	public Object handle(ResultSet rs) throws SQLException {
		ArrayList reportElements = new ArrayList();
		while (rs.next()){
			String classHandler = rs.getString("classHandler");
			ReportElement re = null;
			try {
				Class reportElementClass = Class.forName(classHandler);
				re = (ReportElement) reportElementClass.newInstance();
				// Set values of the form element by calling the setters
				this.setReportElementProperties(re, rs);
			} catch(ClassNotFoundException e){
				// TODO: Handle Exception
				System.out.println(e.toString());
			} catch(IllegalAccessException e){
//				TODO: Handle Exception
				System.out.println(e.toString());
			} catch(InstantiationException e){
//				TODO: Handle Exception
				System.out.println(e.toString());
			}

			reportElements.add(re);
				
		}
		return reportElements;	}

}
